package com.dopies.bsdmall.model;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;

import com.dopies.BeeFramework.model.BaseModel;
import com.dopies.BeeFramework.model.BeeCallback;
import com.dopies.bsdmall.protocol.MESSAGE;
import com.dopies.bsdmall.protocol.PAGINATED;
import com.dopies.bsdmall.protocol.STATUS;
import com.external.androidquery.callback.AjaxStatus;

public class MsgModel extends BaseModel {

	public PAGINATED paginated;
	
	public ArrayList<MESSAGE> msg_list = new ArrayList<MESSAGE>();
    private SharedPreferences shared;
    private SharedPreferences.Editor editor;
    public int unreadCount = 0;

    public static MsgModel Instance;

    public static MsgModel getInstance()
    {
        return Instance;
    }
	public MsgModel(Context context)
    {
		super(context);
        Instance = this;
        shared = context.getSharedPreferences("userInfo", 0);
        editor = shared.edit();
	}

    public void loadCache()
    {
    }

    public int getLatestMessageId()
    {
        return 0;
    }

    public void getUnreadMessageCount()
    {
    	this.unreadCount = 1;
    }

	public void fetchPre()
    {
		String url = ProtocolConst.SHOP_NOTICE;
		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>(){
			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				MsgModel.this.callback(url, jo, status);
				try {
					STATUS responseStatus = STATUS.fromJson(jo.optJSONObject("status"));
					msg_list.clear();
					if(responseStatus.succeed == 1) {
//						JSONArray dataJsonArray = jo.optJSONArray("data");
//						if(dataJsonArray.length()<1 || dataJsonArray == null)
//							return;
//						MESSAGE message = MESSAGE.fromJson(dataJsonArray.getJSONObject(0));
						JSONObject data = jo.optJSONObject("data");
						MESSAGE message = MESSAGE.fromJson(data);
						
						msg_list.add(message);
					}
					MsgModel.this.OnMessageResponse(url, jo, status);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
		};
		
//		SESSION session = SESSION.getInstance();
//		PAGINATION pagination = new PAGINATION();
//		pagination.page = 1;
//		pagination.count = 1;
		JSONObject requestJsonObject = new JSONObject();
		Map<String, String> params = new HashMap<String, String>();
//		try 
//		{
//            requestJsonObject.put("session",session.toJson());
//            requestJsonObject.put("pagination",pagination.toJson());
//		} catch (JSONException e) {
//			// TODO: handle exception
//		}
		 params.put("json",requestJsonObject.toString());
			cb.url(url).type(JSONObject.class).params(params);
			ProgressDialog pd = new ProgressDialog(mContext);
	        pd.setMessage("请稍后...");
			aq.progress(pd).ajax(cb);
    }

    public void fetchNext()
    {

    }
	
}
